(function(a) {
	a.jScrollPane = {active: []};
	a.fn.jScrollPane = function(c) {
		c = a.extend({}, a.fn.jScrollPane.defaults, c);
		var b = function() {
			return false
		};
		return this.each(function() {
			var t = a(this);
			var E = this;
			var an = 0;
			var L;
			var ao;
			var r;
			var ad = c.topCapHeight;
			var j;
			if (a(this).parent().is(".jScrollPaneContainer")) {
				j = a(this).parent();
				an = c.maintainPosition ? t.position().top : 0;
				var q = a(this).parent();
				L = q.innerWidth();
				ao = q.outerHeight();
				a(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown, >.jScrollCap", q).remove();
				t.css({top: 0})
			} else {
				t.data("originalStyleTag", t.attr("style"));
				t.css("overflow", "hidden");
				this.originalPadding = t.css("paddingTop") + " " + t.css("paddingRight") + " " + t.css("paddingBottom") + " " + t.css("paddingLeft");
				this.originalSidePaddingTotal = (parseInt(t.css("paddingLeft")) || 0) + (parseInt(t.css("paddingRight")) || 0);
				L = t.innerWidth();
				ao = t.innerHeight();
				j = a("<div></div>").addClass("jScrollPaneContainer").css({height: ao + "px",width: L + "px"});
				if (c.enableKeyboardNavigation) {
					j.attr("tabindex", c.tabIndex)
				}
				t.wrap(j);
				j = t.parent();
				a(document).bind("emchange", function(ap, aq, p) {
					t.jScrollPane(c)
				})
			}
			r = ao;
			if (c.reinitialiseOnImageLoad) {
				var s = a.data(E, "jScrollPaneImagesToLoad") || a("img", t);
				var i = [];
				if (s.length) {
					s.each(function(p, ap) {
						a(this).bind("load readystatechange", function() {
							if (a.inArray(p, i) == -1) {
								i.push(ap);
								s = a.grep(s, function(at, ar) {
									return at != ap
								});
								a.data(E, "jScrollPaneImagesToLoad", s);
								var aq = a.extend(c, {reinitialiseOnImageLoad: false});
								t.jScrollPane(aq)
							}
						}).each(function(aq, ar) {
							if (this.complete || this.complete === undefined) {
								this.src = this.src
							}
						})
					})
				}
			}
			var Y = this.originalSidePaddingTotal;
			var aj = L - c.scrollbarWidth - c.scrollbarMargin - Y;
			var U = {height: "auto",width: aj + "px"};
			if (c.scrollbarOnLeft) {
				U.paddingLeft = c.scrollbarMargin + c.scrollbarWidth + "px"
			} else {
				U.paddingRight = c.scrollbarMargin + "px"
			}
			t.css(U);
			var W = t.outerHeight();
			var R = ao / W;
			var V = R < 0.99;
			j[V ? "addClass" : "removeClass"]("jScrollPaneScrollable");
			if (V) {
				j.append(a("<div></div>").addClass("jScrollCap").addClass("jScrollCapTop").css({height: c.topCapHeight}), a("<div></div>").addClass("jScrollPaneTrack").css({width: c.scrollbarWidth + "px"}).append(a("<div></div>").addClass("jScrollPaneDrag").css({width: c.scrollbarWidth + "px"}).append(a("<div></div>").addClass("jScrollPaneDragTop").css({width: c.scrollbarWidth + "px"}), a("<div></div>").addClass("jScrollPaneDragBottom").css({width: c.scrollbarWidth + "px"}))), a("<div></div>").addClass("jScrollCap jScrollCapBottom").css({height: c.bottomCapHeight}));
				var ak = a(">.jScrollPaneTrack", j);
				var u = a(">.jScrollPaneTrack .jScrollPaneDrag", j);
				var am;
				var g = [];
				var aa;
				var S = function() {
					if (aa > 4 || aa % 4 == 0) {
						ai(af + am * I)
					}
					aa++
				};
				if (c.enableKeyboardNavigation) {
					j.bind("keydown.jscrollpane", function(p) {
						switch (p.keyCode) {
							case 38:
								am = -1;
								aa = 0;
								S();
								g[g.length] = setInterval(S, 100);
								return false;
							case 40:
								am = 1;
								aa = 0;
								S();
								g[g.length] = setInterval(S, 100);
								return false;
							case 33:
							case 34:
								return false;
							default:
						}
					}).bind("keyup.jscrollpane", function(ap) {
						if (ap.keyCode == 38 || ap.keyCode == 40) {
							for (var p = 0; p < g.length; p++) {
								clearInterval(g[p])
							}
							return false
						}
					})
				}
				if (c.showArrows) {
					var P;
					var y;
					var o = function(p) {
						a("html").unbind("mouseup", o);
						P.removeClass("jScrollActiveArrowButton");
						clearInterval(y)
					};
					var H = function() {
						a("html").bind("mouseup", o);
						P.addClass("jScrollActiveArrowButton");
						aa = 0;
						S();
						y = setInterval(S, 100)
					};
					j.append(a("<a></a>").addClass("jScrollArrowUp").attr({href: "javascript:;",tabindex: -1}).css({width: c.scrollbarWidth + "px",top: c.topCapHeight + "px"}).html("Scroll up").bind("mousedown", function() {
						P = a(this);
						am = -1;
						H();
						this.blur();
						return false
					}).bind("click", b), a("<a></a>").addClass("jScrollArrowDown").attr({href: "javascript:;",tabindex: -1}).css({width: c.scrollbarWidth + "px",bottom: c.bottomCapHeight + "px"}).html("Scroll down").bind("mousedown", function() {
						P = a(this);
						am = 1;
						H();
						this.blur();
						return false
					}).bind("click", b));
					var v = a(">.jScrollArrowUp", j);
					var m = a(">.jScrollArrowDown", j)
				}
				if (c.arrowSize) {
					r = ao - c.arrowSize - c.arrowSize;
					ad += c.arrowSize
				} else {
					if (v) {
						var ab = v.height();
						c.arrowSize = ab;
						r = ao - ab - m.height();
						ad += ab
					}
				}
				r -= c.topCapHeight + c.bottomCapHeight;
				ak.css({height: r + "px",top: ad + "px"});
				var ag = a(this).css({position: "absolute",overflow: "visible"});
				var d;
				var F;
				var I;
				var af = 0;
				var C = R * ao / 2;
				var G = function(ap, ar) {
					var aq = ar == "X" ? "Left" : "Top";
					return ap["page" + ar] || (ap["client" + ar] + (document.documentElement["scroll" + aq] || document.body["scroll" + aq])) || 0
				};
				var O = function() {
					return false
				};
				var ae = function() {
					X();
					d = u.offset({});
					d.top -= af;
					F = r - u[0].offsetHeight;
					I = 2 * c.wheelSpeed * F / W
				};
				var e = function(p) {
					ae();
					C = G(p, "Y") - af - d.top;
					a("html").bind("mouseup", z).bind("mousemove", Q).bind("mouseleave", z);
					if (a.browser.msie) {
						a("html").bind("dragstart", O).bind("selectstart", O)
					}
					return false
				};
				var z = function() {
					a("html").unbind("mouseup", z).unbind("mousemove", Q);
					C = R * ao / 2;
					if (a.browser.msie) {
						a("html").unbind("dragstart", O).unbind("selectstart", O)
					}
				};
				var ai = function(ap) {
					j.scrollTop(0);
					ap = ap < 0 ? 0 : (ap > F ? F : ap);
					af = ap;
					u.css({top: ap + "px"});
					var aq = ap / F;
					t.data("jScrollPanePosition", (ao - W) * -aq);
					ag.css({top: ((ao - W) * aq) + "px"});
					t.trigger("scroll");
					if (c.showArrows) {
						v[ap == 0 ? "addClass" : "removeClass"]("disabled");
						m[ap == F ? "addClass" : "removeClass"]("disabled")
					}
				};
				var Q = function(p) {
					ai(G(p, "Y") - d.top - C)
				};
				var Z = Math.max(Math.min(R * (ao - c.arrowSize * 2), c.dragMaxHeight), c.dragMinHeight);
				u.css({height: Z + "px"}).bind("mousedown", e);
				var T;
				var w;
				var l;
				var ac = function() {
					if (w > 8 || w % 4 == 0) {
						ai((af - ((af - l) / 2)))
					}
					w++
				};
				var al = function() {
					clearInterval(T);
					a("html").unbind("mouseup", al).unbind("mousemove", N)
				};
				var N = function(p) {
					l = G(p, "Y") - d.top - C
				};
				var A = function(p) {
					ae();
					N(p);
					w = 0;
					a("html").bind("mouseup", al).bind("mousemove", N);
					T = setInterval(ac, 100);
					ac();
					return false
				};
				ak.bind("mousedown", A);
				j.bind("mousewheel", function(ap, ar) {
					ar = ar || (ap.wheelDelta ? ap.wheelDelta / 120 : (ap.detail) ? -ap.detail / 3 : 0);
					ae();
					X();
					var aq = af;
					ai(af - ar * I);
					var p = aq != af;
					return !p
				});
				var f;
				var D;
				function J() {
					var p = (f - af) / c.animateStep;
					if (p > 1 || p < -1) {
						ai(af + p)
					} else {
						ai(f);
						X()
					}
				}
				var X = function() {
					if (D) {
						clearInterval(D);
						delete f
					}
				};
				var ah = function(at, p) {
					if (typeof at == "string") {
						try {
							$e = a(at, t)
						} catch (ar) {
							return
						}
						if (!$e.length) {
							return
						}
						at = $e.offset().top - t.offset().top
					}
					X();
					var aq = W - ao;
					at = at > aq ? aq : at;
					t.data("jScrollPaneMaxScroll", aq);
					var ap = at / aq * F;
					if (p || !c.animateTo) {
						ai(ap)
					} else {
						j.scrollTop(0);
						f = ap;
						D = setInterval(J, c.animateInterval)
					}
				};
				t[0].scrollTo = ah;
				t[0].scrollBy = function(ap) {
					var p = -parseInt(ag.css("top")) || 0;
					ah(p + ap)
				};
				ae();
				ah(-an, true);
				a("*", this).bind("focus", function(au) {
					var at = a(this);
					var aw = 0;
					var ap = 100;
					while (at[0] != t[0]) {
						aw += at.position().top;
						at = at.offsetParent();
						if (!ap--) {
							return
						}
					}
					var p = -parseInt(ag.css("top")) || 0;
					var av = p + ao;
					var ar = aw > p && aw < av;
					if (!ar) {
						var aq = aw - c.scrollbarMargin;
						if (aw > p) {
							aq += a(this).height() + 15 + c.scrollbarMargin - ao
						}
						ah(aq)
					}
				});
				if (c.observeHash) {
					if (location.hash && location.hash.length > 1) {
						setTimeout(function() {
							ah(location.hash)
						}, a.browser.safari ? 100 : 0)
					}
					a(document).bind("click", function(ap) {
						$target = a(ap.target);
						if ($target.is("a")) {
							var p = $target.attr("href");
							if (p && p.substr(0, 1) == "#" && p.length > 1) {
								setTimeout(function() {
									ah(p, !c.animateToInternalLinks)
								}, a.browser.safari ? 100 : 0)
							}
						}
					})
				}
				function B(p) {
					a(document).bind("mousemove.jScrollPaneDragging", x);
					a(document).bind("mouseup.jScrollPaneDragging", n)
				}
				var M;
				var h;
				function K() {
					direction = M < 0 ? -1 : 1;
					t[0].scrollBy(M / 2)
				}
				function k() {
					if (h) {
						clearInterval(h);
						h = undefined
					}
				}
				function x(aq) {
					var ar = t.parent().offset().top;
					var p = ar + ao;
					var ap = G(aq, "Y");
					M = ap < ar ? ap - ar : (ap > p ? ap - p : 0);
					if (M == 0) {
						k()
					} else {
						if (!h) {
							h = setInterval(K, 100)
						}
					}
				}
				function n(p) {
					a(document).unbind("mousemove.jScrollPaneDragging").unbind("mouseup.jScrollPaneDragging");
					k()
				}
				j.bind("mousedown.jScrollPane", B);
				a.jScrollPane.active.push(t[0])
			} else {
				t.css({height: ao + "px",width: L - this.originalSidePaddingTotal + "px",padding: this.originalPadding});
				t[0].scrollTo = t[0].scrollBy = function() {
				};
				t.parent().unbind("mousewheel").unbind("mousedown.jScrollPane").unbind("keydown.jscrollpane").unbind("keyup.jscrollpane")
			}
		})
	};
	a.fn.jScrollPaneRemove = function() {
		a(this).each(function() {
			$this = a(this);
			var b = $this.parent();
			if (b.is(".jScrollPaneContainer")) {
				$this.css({top: "",height: "",width: "",padding: "",overflow: "",position: ""});
				$this.attr("style", $this.data("originalStyleTag"));
				b.after($this).remove()
			}
		})
	};
	a.fn.jScrollPane.defaults = {scrollbarWidth: 10,scrollbarMargin: 5,wheelSpeed: 18,showArrows: false,arrowSize: 0,animateTo: false,dragMinHeight: 1,dragMaxHeight: 99999,animateInterval: 100,animateStep: 3,maintainPosition: true,scrollbarOnLeft: false,reinitialiseOnImageLoad: false,tabIndex: 0,enableKeyboardNavigation: true,animateToInternalLinks: false,topCapHeight: 0,bottomCapHeight: 0,observeHash: true};
	a(window).bind("unload", function() {
		var c = a.jScrollPane.active;
		for (var b = 0; b < c.length; b++) {
			c[b].scrollTo = c[b].scrollBy = null
		}
	})
})(jQuery);